<?php
/**
 * RSA 加解密
 *
 * @author suning <snsnsky@gmail.com>
 *
 * $Id$
 */

namespace Encrypt;

class Rsa
{
    /**
     * 公钥
     *
     * @var string
     */
    private static $public_key = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArab2jhyLLBcsL9wV0XVg
UphaF2yzcz51ZSRK+5XI3aq2w59gafx8URXyq4iM1Mh9Oql1nvrlTt2NFFZ1R/OQ
vvqKXzdd/dL9LUrvUyJiNGzVOPmw0sSAUtG3VDO1oPa1h1+9b9d17C0g74llOdCq
6QmvnUFRaGeTmoN3GZ+w/Of0ZN5SXs6fuxXIs1g35lHFhbvvOxOT6zCg8Q0x9SCf
c7KMh2znL0rK6TCbBVHExmR5jISqhUKRwGGjQIEOnBK4Kx5dsdB2YpCi7ficKYP3
hc6+CdoQFlGqPpXIaBupgA6NJF/UQ8XP5trxod7gWzl9oABbZmzI9K7f/Mi45sWr
FwIDAQAB
-----END PUBLIC KEY-----';

    /**
     * 私钥
     *
     * @var string
     */
    private static $private_key = '-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArab2jhyLLBcsL9wV0XVgUphaF2yzcz51ZSRK+5XI3aq2w59g
afx8URXyq4iM1Mh9Oql1nvrlTt2NFFZ1R/OQvvqKXzdd/dL9LUrvUyJiNGzVOPmw
0sSAUtG3VDO1oPa1h1+9b9d17C0g74llOdCq6QmvnUFRaGeTmoN3GZ+w/Of0ZN5S
Xs6fuxXIs1g35lHFhbvvOxOT6zCg8Q0x9SCfc7KMh2znL0rK6TCbBVHExmR5jISq
hUKRwGGjQIEOnBK4Kx5dsdB2YpCi7ficKYP3hc6+CdoQFlGqPpXIaBupgA6NJF/U
Q8XP5trxod7gWzl9oABbZmzI9K7f/Mi45sWrFwIDAQABAoIBAAtgsyrgh+XdptGD
9BIDRMpknRKH1sIoR3zXAeq2B7EubEco/CI1nteZPosqqT+P21SqocBG5BtkvMem
WozaUpOQyt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0NZKTE2S7OlB3FYYbljr28oXlZ75BqzK04Iz
DUSSgguXB9Tx4GlNbPNfeEcBwEV6nXwyDTWNshE5+LKtFAcITF+JcA==
-----END RSA PRIVATE KEY-----
';

    /**
     * 私钥解密
     *
     * @param string $code    加密代码
     * @param string $key     私钥, 可选
     * @return bool|string    解密成功返回明文, 失败返回false
     */
    public static function private_decode($code, $key = '')
    {
        openssl_private_decrypt(base64_decode($code), $private_decode, $key == '' ? self::$private_key : $key);
        return empty($private_decode) ? false : $private_decode;
    }

    /**
     * 私钥加密
     *
     * @param string $data    明文
     * @param string $key     私钥, 可选
     * @return bool|string    加密成功返回密文, 失败返回false
     */
    public static function private_encode($data, $key = '')
    {
        openssl_private_encrypt($data, $private_encode, $key == '' ? self::$private_key : $key);
        return empty($private_encode) ? false : base64_encode($private_encode);
    }

    /**
     * 公钥解密
     *
     * @param string $code    加密代码
     * @param string $key     公钥, 可选
     * @return bool|string    解密成功返回明文, 失败返回false
     */
    public static function public_decode($code, $key = '')
    {
        openssl_public_decrypt(base64_decode($code), $public_decode, $key == '' ? self::$public_key : $key);
        return empty($public_decode) ? false : $public_decode;
    }

    /**
     * 公钥加密
     *
     * @param string $data    明文
     * @param string $key     公钥, 可选
     * @return bool|string    加密成功返回密文, 失败返回false
     */
    public static function public_encode($data, $key = '')
    {
        openssl_public_encrypt($data, $public_encode, $key == '' ? self::$public_key : $key);
        return empty($public_encode) ? false : base64_encode($public_encode);
    }
}